﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using Microsoft.Phone.Tasks;

namespace AllenalaMente.Menu
{
    public partial class CarouselBanner : UserControl
    {
        private DispatcherTimer timer = new DispatcherTimer();

        private MarketplaceDetailTask detailTask;
        private string gameId;

        public CarouselBanner()
        {
            InitializeComponent();

            detailTask = new MarketplaceDetailTask();

            Random rnd = new Random();

            SetImage("Banner0" + rnd.Next(1, 11));

            this.MouseLeftButtonUp += imageBanner_MouseLeftButtonUp;

            // Timer
            timer.Tick -= Update;
            timer.Interval = TimeSpan.FromSeconds(30);
            timer.Tick += Update;
            timer.Start();
        }

        private void Update(object sender, EventArgs e)
        {
            Random rnd = new Random();

            SetImage("Banner0" + rnd.Next(1, 11));
        }

        void imageBanner_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            detailTask.ContentIdentifier = AllenalaMente.Assets.GamesId.ResourceManager.GetString(gameId);
            detailTask.Show();
        }

        public void SetImage(string imm)
        {
            FadeIn(imageBanner);

            gameId = imm;

            string ImagePath = "/AllenalaMente;component/Assets/Images/Banner/" + imm + ".jpg";

            Uri uri = new Uri(ImagePath, UriKind.RelativeOrAbsolute);
            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
            imageBanner.SetValue(Image.SourceProperty, img);
        }

        // FADE IN
        public void FadeIn(UIElement elem)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = 0;
            doubleAnimation.To = 1;
            doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));

            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
            Storyboard.SetTarget(doubleAnimation, elem);

            Storyboard sb = new Storyboard();
            sb.Children.Add(doubleAnimation);
            sb.Begin();
        }
    }
}
